const deptService = require('../services/deptService');
const { success, successWithData, error } = require('../utils/resultwrapper');

class DeptController {
    /**
     * 创建部门
     */
    async createDept(req, res) {
        try {
            const deptData = req.body;
            const result = await deptService.createDept(deptData);
            res.json(successWithData(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 更新部门
     */
    async updateDept(req, res) {
        try {
            const { id } = req.params;
            const deptData = req.body;
            const result = await deptService.updateDept(id, deptData);
            res.json(successWithData(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 删除部门
     */
    async deleteDept(req, res) {
        try {
            const { id } = req.params;
            const result = await deptService.deleteDept(id);
            res.json(success(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 获取部门详情
     */
    async getDeptById(req, res) {
        try {
            const { id } = req.params;
            const result = await deptService.getDeptById(id);
            res.json(successWithData(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 获取部门列表
     */
    async getDeptList(req, res) {
        try {
            const result = await deptService.getDeptList(req.query);
            res.json(successWithData(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 获取部门树形结构
     */
    async getDeptTree(req, res) {
        try {
            const { keywords, status } = req.query;
            const result = await deptService.getDeptTree({ keywords, status });
            res.json(successWithData(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 获取部门下拉选项
     */
    async getDeptOptions(req, res) {
        try {
            const result = await deptService.getDeptOptions();
            res.json(successWithData(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }
}

module.exports = new DeptController();